﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MiniFox.Data.NativeClient
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public abstract class NativeDataCommand<TEntity> : DataCommand<TEntity> where TEntity : class
    {
        /// <summary>
        /// 
        /// </summary>
        Database _database;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="database"></param>
        public NativeDataCommand(Database database)
        {
            _database = database;
            _database.ExecuteTimeout = this.TimeOut;
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual Database Database
        {
            get { return _database; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected string DbSchema
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public override string ConnectionString
        {
            get
            {
                return this.Database.ConnectionString;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectName"></param>
        /// <returns></returns>
        protected string FormatDatabaseObject(string objectName)
        {
            return string.IsNullOrEmpty(this.DbSchema) ? objectName : $"{this.DbSchema}.{objectName}";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        protected string FormatKeyValueParameter(string columnName, string parameterName)
        {
            return $"{columnName}={parameterName}";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyValuePair"></param>
        /// <returns></returns>
        protected string FormatKeyValueParameter(KeyValuePair<string, object> keyValuePair)
        {
            string val = "null";

            if (keyValuePair.Value != null && keyValuePair.Value != DBNull.Value)
            {
                var code = Convert.GetTypeCode(keyValuePair.Value);
                switch (code)
                {
                    case TypeCode.Object:
                        val = $"'{keyValuePair.Value}'";
                        break;
                    case TypeCode.String:
                        val = $"'{keyValuePair.Value}'";
                        break;
                    case TypeCode.Char:
                        val = $"'{keyValuePair.Value}'";
                        break;
                    case TypeCode.DateTime:
                        DateTime dateTime = (DateTime)keyValuePair.Value;
                        val = $"'{dateTime.ToString(GlobalStandardFormat.SIMPLE_DATETIME_FORMAT)}'";
                        break;
                    default:
                        val = keyValuePair.Value.ToString();
                        break;

                }
            }
            return $"{keyValuePair.Key}={val}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlStatement"></param>
        /// <returns></returns>
        protected virtual string CreateSqlStatement(params string[] sqlStatement)
        {
            if (sqlStatement.Length == 1)
                return sqlStatement[0];
            for (int i = 0; i < sqlStatement.Length; i++)
            {
                string str = sqlStatement[i];
                if (str.EndsWith(";"))
                    continue;

                sqlStatement[i] = $"{str};";
            }
            return string.Join(@"\r", sqlStatement);
        }
    }
}
